package router

import (
	"4-11zk/client/client"
	"4-11zk/client/requset"
	"4-11zk/client/response"
	"4-11zk/common/proto/user"
	"github.com/gin-gonic/gin"
)

func Login(c *gin.Context) {
	var data requset.Login
	err := c.ShouldBind(&data)
	if err != nil {
		response.ResponseDataError(c, err.Error())
		return
	}
	login, err := client.Login(c, &user.LoginReq{
		Phone:    data.Phone,
		Password: data.Password,
	})
	if err != nil {
		response.ResponseDataError(c, err.Error())
		return
	}
	response.ResponseDataSuccess(c, login)
}

func PlanAdd(c *gin.Context) {
	userid := c.GetUint("userId")
	var data requset.PlanAdd
	err := c.ShouldBind(&data)
	if err != nil {
		response.ResponseDataError(c, err.Error())
		return
	}
	add, err := client.PlanAdd(c, &user.PlanAddReq{
		CouResId: int64(data.CouResId),
		UserId:   int64(userid),
	})
	if err != nil {
		response.ResponseDataError(c, err.Error())
		return
	}
	response.ResponseDataSuccess(c, add)
}

func PlanDel(c *gin.Context) {
	var data requset.PlanDel
	err := c.ShouldBind(&data)
	if err != nil {
		response.ResponseDataError(c, err.Error())
		return
	}
	del, err := client.PlanDel(c, &user.PlanDelReq{Id: int64(data.Id)})
	if err != nil {
		response.ResponseDataError(c, err.Error())
		return
	}
	response.ResponseDataSuccess(c, del)
}

func OrderAdd(c *gin.Context) {
	userid := c.GetUint("userId")
	var data requset.OrderCreate
	err := c.ShouldBind(&data)
	if err != nil {
		response.ResponseDataError(c, err.Error())
		return
	}
	add, err := client.OrderAdd(c, &user.OrderAddReq{
		UserId:  int64(userid),
		GoodsId: int64(data.GoodsId),
	})
	if err != nil {
		response.ResponseDataError(c, err.Error())
		return
	}
	response.ResponseDataSuccess(c, add)
}

func OrderCall(c *gin.Context) {
	value := c.PostForm("trade_status")
	form := c.PostForm("out_trade_no")
	call, err := client.OrderCall(c, &user.OrderCallReq{
		OrderSn: form,
		Status:  value,
	})
	if err != nil {
		response.ResponseDataError(c, err.Error())
		return
	}
	response.ResponseDataSuccess(c, call)
}
